package com.guigu.servicebusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.config.CacheKey;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicebusiness.entity.TbAd;
import com.guigu.servicebusiness.mapper.TbAdMapper;
import com.guigu.servicebusiness.service.ITbAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@Service
public class TbAdServiceImpl extends ServiceImpl<TbAdMapper, TbAd> implements ITbAdService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbAd> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbAd> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,wrapper);
        if (pageAlbum.getRecords() == null && pageAlbum.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("albumRows",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }

    //从缓存中查询广告列表
    @Override
    public List<TbAd> findByPosition(String position) {
        return (List<TbAd>)redisTemplate.boundHashOps(CacheKey.AD).get(position);
    }

    /**
     * 新增
     * @param tbAd
     */
    @Override
    public void insert(TbAd tbAd) {
        baseMapper.insert(tbAd);
        saveAdToRedisByPosition(tbAd.getPosition());//重新加载缓存
    }
    /**
     * 修改
     * @param tbAd
     */
    @Override
    public void ByIdupdate(TbAd tbAd) {
        String position = baseMapper.selectById(tbAd.getId()).getPosition();
        baseMapper.updateById(tbAd);
        saveAdToRedisByPosition(position);//更新
        if(!position.equals(tbAd.getPosition())){ //如果广告位置发生变化
            saveAdToRedisByPosition(tbAd.getPosition());
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void ByIdremove(Integer id) {
        String position = baseMapper.selectById(id).getPosition();
        saveAdToRedisByPosition(position);//重新加载缓存
        baseMapper.deleteById(id);
    }

    @Override
    public void deletelist(List<TbAd> list) {
        for (TbAd o:list){
            String position = baseMapper.selectById(o.getId()).getPosition();
            saveAdToRedisByPosition(position);
            baseMapper.deleteById(o.getId());
        }
    }

    @Override
    public void saveAdToRedisByPosition(String position) {
        //查询某位置的广告列表
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("position",position);
        wrapper.le("start_time",new Date());//开始时间小于等于当前时间
        wrapper.ge("end_time",new Date());//截至时间大于等于当前时间
        wrapper.eq("status","1");
        List<TbAd> adList = baseMapper.selectList(wrapper);//装入缓存
        redisTemplate.boundHashOps(CacheKey.AD).put(position,adList);
    }

    @Override
    public void saveAllAdToRedis() {
        //循环所有的广告位置
        for(String position:getPositionList()){
            saveAdToRedisByPosition(position);
        }
    }

    /**
     * 返回所有的广告位置
     * @return
     */
    private List<String> getPositionList(){
        List<String> positionList=new ArrayList<String>();
        positionList.add("web_index_lb");//首页广告轮播图
        //。。。
        return positionList;
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbAd> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbAd> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("name")!=null &&!"".equals(searchMap.get("name"))){
                wrapper.like("name","%"+searchMap.get("name")+"%");
            }
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("startTime")!=null && !"".equals(searchMap.get("startTime")))
                wrapper.ge("start_time",searchMap.get("startTime"));
            if(searchMap.get("endTime")!=null && !"".equals(searchMap.get("endTime")))
                wrapper.le("end_time",searchMap.get("endTime"));

        }
        return wrapper;
    }


}
